package com.globagames.strategicgames.business
{
	import com.globagames.isometric.core.view.IsoMap;
	import com.globagames.isometric.core.view.IsoOperation;
	import com.globagames.strategicgames.model.Soldier;
	import com.globagames.strategicgames.model.StrategicGameModelLocator;
	
	import mx.messaging.Consumer;
	import mx.messaging.events.MessageEvent;
	import mx.messaging.events.MessageFaultEvent;
	
	public class MessageConsumer extends Consumer
	{
		
		private var model:StrategicGameModelLocator = StrategicGameModelLocator.getInstance();
		public var isomap:IsoMap;
	
		public function MessageConsumer()
		{
			destination = "battleMessages";
			subtopic = ""+model.opponentCompany.companyId + "_"+model.company.companyId;				
			addEventListener(MessageEvent.MESSAGE, messageHandler);
			addEventListener(MessageFaultEvent.FAULT, faultHandler);
			subscribe();
		}
		public function messageHandler(event:MessageEvent)
		{
			trace("messageHandler "+event.message.body);
			var s:Array =event.message.body.split("_");
			var action:String = s[0];
			if( action == IsoOperation.ADD_TO_DISPLAY_LIST ) {
				var soldier:Soldier = model.opponentCompany.getSoldier(s[1]);
				soldier.soldierIso.style = 2;
				isomap.addIsoObject(soldier.soldierIso,soldier.soldierId);
			} else if (action == IsoOperation.PLACE_ISO_OBJECT ) {
				isomap.operations.push(event.message.body);
			} else if (action == "disconnected" ) {
				model.globalState = "OPPONENT_DISCONNECTED";
				
			}
			isomap.doRetrace = true;
			isomap.invalidateProperties();						
		}
		public function faultHandler(event:MessageFaultEvent)
		{
			trace("faultHandler");
		}		
	}
}